package com.wxh.new_erp.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxh.new_erp.sys.common.DataGridView;
import com.wxh.new_erp.sys.common.ResultObj;
import com.wxh.new_erp.sys.domian.Loginfo;
import com.wxh.new_erp.sys.service.LoginfoService;
import com.wxh.new_erp.sys.vo.LoginfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("loginfo")
public class LoginfoController {

    @Autowired
    private LoginfoService loginfoService;


    /**
     *日志查询分页
     * @param loginfoVo
     * @return
     */
    @RequestMapping("loadAllLoginfo")
    public DataGridView loadAllLoginfo(LoginfoVo loginfoVo){


        IPage <Loginfo>  page=new Page<>(loginfoVo.getPage(),loginfoVo.getLimit());
        QueryWrapper <Loginfo>queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(loginfoVo.getLoginname()),"loginname",loginfoVo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(loginfoVo.getLoginip()),"loginip",loginfoVo.getLoginip());

        queryWrapper.ge(loginfoVo.getStartTime()!=null ,"logintime",loginfoVo.getStartTime());
        queryWrapper.le(loginfoVo.getEndTime()!=null,"logintime",loginfoVo.getEndTime());

        queryWrapper.orderByDesc("logintime");
        this.loginfoService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 删除
     * @param loginfoVo
     * @return
     */
    @RequestMapping("deleteLoginfo" )
    public ResultObj deleteLoginfo(LoginfoVo loginfoVo){
        try {
            loginfoService.removeById(loginfoVo.getId());
            return  ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param loginfoVo
     * @return
     */
    @RequestMapping("batchDeleteLoginfo")
    public  ResultObj batchDeleteLoginfo(LoginfoVo loginfoVo){
        try {
            Collection<Integer> collection=new ArrayList<>();
            for (Integer id:loginfoVo.getIds()){
                collection.add(id);
            }
            this.loginfoService.removeByIds(collection);
            return  ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultObj.DELETE_ERROR;
        }
    }
}
